package com.tao.runner;

import com.tao.constants.SystemConstant;
import com.tao.dao.HelpMapper;
import com.tao.entity.Help;
import com.tao.netty.IMNettyServer;
import com.tao.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author  诸葛先生
 * 功能：SpringBoot项目启动预处理
 */
@Component
public class ViewCountRunner implements CommandLineRunner {

    @Autowired
    HelpMapper helpMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        //查询求助信息   id   viewCount
        List<Help> helps = helpMapper.selectList(null);
        Map<String, Integer> viewCount = helps.stream()
                .collect(Collectors.toMap(help -> help.getId().toString(), Help::getViewCount));
        //存储到redis中
        redisCache.setCacheMap(SystemConstant.HELP_VIEW_COUNT,viewCount);
        runServer();
    }

    public static void runServer() throws Exception {
        IMNettyServer.start(8888);
//        TestNettyServer.start(8888);
    }
}
